// 商品类目
<template>
  <div class="role">
    <div class="role-content">
      <div class="role-top">
        <el-input
          placeholder="请输入类目id"
          v-model="paging.id"
          clearable
        ></el-input>
        <el-input
          placeholder="请输入类目名称"
          v-model="paging.title"
          clearable
        ></el-input>
        <el-select v-model="paging.level" placeholder="请选择类目级别">
          <el-option
            v-for="item in levelData"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          
          </el-option>
        </el-select>
         <el-cascader   style='margin-right: 10px !important;'
              ref="cascader"
              :options="options"
              :props="{ checkStrictly: true, value: 'value', label: 'label' }"
              v-model="ruleForm.parentId"
              @change="handleChange"
              placeholder="请选择父类目"
            ></el-cascader>
        <el-button type="primary" icon="el-icon-search" @click="role()"
          >搜索</el-button
        >
        <el-button type="primary" icon="el-icon-edit" @click="addEquipment()"
          >添加</el-button
        >
      </div>
      <!--内容 -->
      <el-card class="box-card">
        <el-table :data="listData" style="width: 100%" border>
          <el-table-column prop="value" label="类目id" sortable>
          </el-table-column>
          <el-table-column prop="label" label="类目名">
            <template slot-scope="scope">
              <el-tag>{{ scope.row.label }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="fullName" label="全类目名">
            <template slot-scope="scope">
              <el-tag>{{ scope.row.fullName }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="父类目id">
            <template slot-scope="scope">
              <el-tag>{{ scope.row.parent }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="级别">
            <template slot-scope="scope">
              <el-tag>{{ scope.row.level | levelData() }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="标准图片">
            <template slot-scope="scope">
              <img
                :src="scope.row.iconUrl"
                alt=""
                style="width: 80px; height: 70px"
              />
            </template>
          </el-table-column>
          <el-table-column label="类目图片">
            <template slot-scope="scope">
              <img
                :src="scope.row.picUrl"
                alt=""
                style="width: 80px; height: 70px"
              />
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" @click="handelEdit(scope.row)" class="blue"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 分页 -->
      <div class="block">
        <!-- current-change当前页size-change每页条数current-page当前页数，支持 .sync 修饰符page-sizes每页显示条目个数，支持 .sync 修饰符-->
        <Paging @handle="handles" :allPages="paging"></Paging>
      </div>
      <!-- 添加编辑 -->
      <el-dialog
        :title="titleMap[dialogStatus]"
        :visible.sync="dialogFormVisible"
      >
        <el-form :model="ruleForm" ref="ruleForm" :rules="rules">
          <el-form-item
            label="类目名称"
            prop="title"
            :label-width="formLabelWidth"
            style="margin-left: 0"
          >
            <el-input v-model="ruleForm.title" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            label="类目图片"
            :label-width="formLabelWidth"
            style="margin-bottom: 0px"
          >
               <el-upload
                    class="avatar-uploader"
                    action="http://192.168.1.84:8081/upload/admin"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload"
                    >
                    <img v-if="ruleForm.picUrl" :src="ruleForm.picUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-upload>
          </el-form-item>
          <!-- :props="{ checkStrictly: true,value:'id',label:'label'}"  -->
          <el-form-item
            label="父类目"
            :label-width="formLabelWidth"
            class="relevance"
          >
            <el-cascader
              ref="cascader"
              :options="options"
              :props="{ checkStrictly: true, value: 'value', label: 'label' }"
              v-model="ruleForm.parentId"
              @change="handleChange"
              placeholder="关联产品"
            ></el-cascader>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="affirm(ruleForm)">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import goods from "../../api/goods/goodscategory";
import Paging from "../../components/Paging"; //分页
export default {
  components: {
    Paging,
  },
  data() {
    return {
      imageUrl: "",
      levelData: [
        {
          value: "0",
          label: "一级类目",
        },
        {
          value: "1",
          label: "二级类目",
        },
        {
          value: "2",
          label: "三级类目",
        },
      ],
      options: [], //spu树
      // 分页
      paging: {
        id: "",
        title: "",
        level: "",
        parentId: "",
        currentPage: 1, //当前的默认的页数
        eachpage: 20, //默认的页码
        total: 0, //初始值共数据条
        totalPageNo: 1,
      },
      listData: [], //初始化数据
      dialogFormVisible: false,
      FormVisible: false, //弹窗口
      select: [], //选中的权限'
      data: [],
      // 弹框
      dialogStatus: "",
      titleMap: {
        addEquipment: "新增",
        editEquipment: "编辑",
      },
      //  输入框宽度
      formLabelWidth: "120px",
      //  输入框
      ruleForm: {
        title: "",
        picUrl: "",
        id: "",
        adId: "",
        parentId: "",
      },
      rules: {
        title: [
          { required: true, message: "广告标题不能为空", trigger: "blur" },
        ],
      },
      currentPage: "",
      val: [],
    };
  },
  filters: {
    adTypes: function (value) {
      switch (value) {
        case 4:
          value = "首页轮播下5按钮";
          break;
        case 2:
          value = "分类精选";
          break;
        case 1:
          value = "轮播";
          break;
        case 3:
          value = "横幅";
          break;
        case 1:
          value = "轮播";
          break;
      }
      return value;
    },
    levelData: function (value) {
      switch (value) {
        case 0:
          value = "一级类目";
          break;
        case 1:
          value = "二级类目";
          break;
        case 2:
          value = "三级类目";
      }
      return value;
    },
  },
  created() {
    this.role();
    this.spUcategory();
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.ruleForm.picUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;
        // 图片类型
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
  
    handleChange(val) {
      this.ruleForm.parentId = this.$refs.cascader.getCheckedNodes()[0].data.value; //获取选中的节点数组
      console.log(this.ruleForm.parentId)
    },
    // 分页负值
    handles(val) {
      this.allPages = val;
      this.role();
    },
    //数据详情
    role() {
      let _this = this;
      goods.category(_this.paging,_this.ruleForm.parentId).then((res) => {
        _this.listData = res.data.data.items;
        _this.ruleForm = { ...res.data.data.items };
        _this.paging.currentPage = res.data.data.pageNo;
        _this.paging.eachpage = res.data.data.pageSize;
        _this.paging.total = res.data.data.total;
        _this.paging.totalPageNo = res.data.data.totalPageNo;
      });
    },
    // SPU树
    spUcategory() {
      goods.spuTree().then((res) => {
        this.options = res.data.data;
      });
    },
    //递归判断列表，把最后的children设为undefined
    getTreeData(data){
      for(var i=0;i<data.length;i++){
        if(data[i].children.length<1){
          // children若为空数组，则将children设为undefined
          data[i].children=undefined;
        }else {
          // children若不为空数组，则继续 递归调用 本方法
          this.getTreeData(data[i].children);
        }
      }
      return data;
    },
    // 删除
    handleDelete(row) {
      let _this = this;
      _this
        .$confirm("此操作将永久删除, 是否继续?", "提示", {
          // 提示框
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          goods.deletes(row).then((res) => {
            if (res.data.errmsg == "成功") {
              _this.$message.success("恭喜你，删除成功");
            } else {
              _this.$message.error(res.data.errmsg);
            }
            _this.role();
          });
        });
    },
    // 添加确认按钮
    affirm(ruleForm) {
      let _this = this;
      _this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.dialogStatus == "addEquipment") {
            goods.addition(_this.ruleForm).then((res) => {
              if (res.data.errmsg == "成功") {
                _this.$message.success("恭喜你，添加成功");
              } else {
                _this.$message.error(res.data.errmsg);
              }
              _this.dialogFormVisible = false;
              _this.role();
            });
          }
            let data={
                id:_this.ruleForm.id,
                title:_this.ruleForm.title,
                parentId:_this.ruleForm.parentId,
                picUrl:_this.ruleForm.picUrl,
            }
          if (_this.dialogStatus == "editEquipment") {
              goods.compile(data).then((res) => {
                if (res.data.errmsg == "成功") {
                  _this.$message.success("恭喜你，更新广告成功");
                   _this.dialogFormVisible = false;
                   _this.role();
                } else {
                  _this.$message.error(res.data.errmsg);
                }
              });
          }
        } else {
          _this.$message.error("输入框不能为空！");
          return false;
        }
      });
    },
    // 添加
    addEquipment() {
      this.dialogFormVisible = true;
      this.dialogStatus = "addEquipment";
      this.ruleForm = {};
    },
    // 编辑
    handelEdit(row) {
      console.log(row);
      this.dialogFormVisible = true;
      this.dialogStatus = "editEquipment";
      this.ruleForm = { ...row };
      this.ruleForm.title = row.label;
      this.ruleForm.parentId = row.parent;
      this.ruleForm.id = row.value;
    },
  },
  watch: {
    // 去除错误提示
    dialogFormVisible(val) {
      if (val == false) {
        this.$refs.ruleForm.clearValidate();
      }
    },
  },
};
</script>
<style scoped>
.role {
  width: 100%;
  line-height: 0px;
  overflow: hidden;
}
/* 上传 */
.relevance >>> .el-form-item__label {
  padding-right: 30px;
}
.avatar-uploader/deep/.el-upload {
  border: 1px dashed #cccccc;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader/deep/.el-icon-plus {
  width: 100px;
  height: 100px;
  line-height: 150px;
}
>>> .el-upload {
  width: 150px;
  height: 150px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
>>> .avatar-uploader {
  width: 150px;
  margin-bottom: 10px;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
/* 添加编辑 */
.el-input {
  width: 300px;
}
.el-form/deep/.el-input__inner {
  width: 300px;
}
.el-form/deep/.el-form-item__content {
  width: 300px;
  margin-left: 0 !important;
}
>>> .el-dialog/deep/.el-dialog__header {
  width: 50px;
}
.el-form-item/deep/.el-form-item__error {
  text-indent: 8rem;
}
/* 分页 */
.block {
  width: 40%;
  padding-top: 20px;
}
/* 字体居中 */
.blue {
  background: #409eff;
  color: #ffffff;
}
.el-table--enable-row-transition/deep/.el-table__body td {
  text-align: center;
}
.el-table/deep/th.is-sortable {
  cursor: pointer;
  text-align: center;
}
.el-table td,
.el-table/deep/th.is-leaf {
  text-align: center;
}
/* 布局 */
.el-card/deep/.box-card {
  height: 50%;
  overflow: hidden;
}
.role-content {
  width: 96%;
  margin: auto;
  overflow: hidden;
}
.el-input {
  width: 200px;
  margin-right: 10px;
}
.role-top {
  width: 300px;
  line-height: 60px;
}
.role-top/deep/.el-select {
  margin-right: 10px;
}
.box-card {
  overflow: hidden;
}
</style>
